package com.mall.service.authority.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.common.CommonResult;
import com.mall.model.domain.UmsMenu;
import com.mall.model.vo.PageResult;
import com.mall.model.vo.UmsMenuVo;
import com.mall.service.authority.service.UmsMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/menu")
@Slf4j
public class UmsMenuController {

    @Autowired
    private UmsMenuService umsMenuService;


    /**
     * 添加后台菜单
     *
     * @return
     */
    @PostMapping("create")
    public CommonResult create(@RequestBody UmsMenu umsMenu) {
        //判断存入的等级
        if (umsMenu.getParentId() == 0) {
            umsMenu.setLevel(0);
        } else {
            umsMenu.setLevel(1);
        }

        Date date = new Date();//创建一个date对象保存当前时间
        umsMenu.setCreateTime(date);
        umsMenuService.save(umsMenu);
        return CommonResult.success("添加成功");
    }

    /**
     * 根据ID删除后台菜单
     */
    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Long id) {
        umsMenuService.removeById(id);
        return CommonResult.success("删除成功!");
    }

    /**
     * 分页查询后台订单
     */
    @GetMapping("/list/{parentId}")
    public CommonResult List(@PathVariable Long parentId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
        Page<UmsMenu> umsMenuPage = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<UmsMenu> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(parentId != null, UmsMenu::getParentId, parentId);

        queryWrapper.orderByAsc(UmsMenu::getCreateTime);

        Page<UmsMenu> pageInfo = umsMenuService.page(umsMenuPage, queryWrapper);

        PageResult pr = new PageResult(pageNum, pageSize, (int) pageInfo.getTotal(), pageInfo.getRecords());

        return CommonResult.success(pr);
    }

    /**
     * 修改后台菜单
     */
    @PostMapping("update/{id}")
    public CommonResult update(@RequestBody UmsMenu umsMenu, @PathVariable Long id) {
        umsMenu.setId(id);
        Date date=new Date();
        umsMenu.setCreateTime(date);
        umsMenuService.updateById(umsMenu);
        return CommonResult.success("更新成功");
    }

    /**
     * 修改菜单显示状态
     *
     * @param
     * @param id
     * @return
     */
    @PostMapping("updateHidden/{id}")
    public CommonResult updateHidden(@PathVariable Long id, @RequestParam Integer hidden) {
        UmsMenu umsMenu = umsMenuService.getById(id);
        umsMenu.setHidden(hidden);
        Date date=new Date();
        umsMenu.setCreateTime(date);
        umsMenuService.updateById(umsMenu);
        return CommonResult.success("更新成功");
    }

    /**
     * 根据ID获取菜单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult umsMenuFindById(@PathVariable Long id) {
        UmsMenu umsMenu = umsMenuService.getById(id);
        return CommonResult.success(umsMenu, "查询成功");
    }

    /**
     * 树形结构返回所有菜单列表
     *
     * @return
     */
    @GetMapping("/treeList")
    public CommonResult treeList() {
        List<UmsMenuVo> list = umsMenuService.treeList();
        return CommonResult.success(list);
    }
}
